const { ipcRenderer } = require('electron')
const { $ } = require('./helper')
const path = require('path')

let musicFilesPath;

$('select-music-btn').addEventListener('click', () => {
    ipcRenderer.send('open-music-file')
})

$('save-music-btn').addEventListener('click', () => {
    if (musicFilesPath == null || musicFilesPath.lenght == 0) {
        console.log('没有可添加的数据!');
        return;
    }
    ipcRenderer.send('save-tracks', musicFilesPath)
})

// 渲染列表
const renderListHTML = (pathes) => {
    const musicList = $('musicList')
    const musicItemHtml = pathes.reduce((html, music) => {
        html += `<li class="list-group-item">${path.basename(music)}</li>`
        return html
    }, '')
    musicList.innerHTML = `<ul class="list-group" >${musicItemHtml}</ul>`
}


// 选择文件事件
ipcRenderer.on('selected-file', (event, path) => {
    if (Array.isArray(path)) {
        renderListHTML(path)
        musicFilesPath = path;
    }
})